import { Config } from "./types/Config";
import { Base } from "../core/Base";
import { Engine } from "../Engine";
// import { textureLoader } from "../assets/loader/TextureLoader";

class ParamManager extends Base {
    constructor(opts = {}, share) {
        super(opts);
        this.params = new Map();
        if (typeof share === 'object') {
            this.shareParams(share);
        }
    }
    shareParams(share) {
        Object.entries(share).forEach(([id, value]) => {
            this.addParam(id, value);
            value.bind(this);
        });
        return this;
    }
    addParam(id, parameter) {
        if (!this.has(id)) {
            this.params.set(id, parameter);
            if (parameter) {
                parameter.state = false;
                parameter.bind(this);
            }
        } else {
            const param = this.getParam(id);
            if (param) {
                param.state = false;
                param.bind(this);
            } else {
                if (parameter) {
                    this.params.set(id, parameter);
                    parameter.state = false;
                    parameter.bind(this);
                }
            }
        }
        return this;
    }
    addConfigParam(id, value) {
        if (!this.has(id)) {
            let param = new Config({
                name: id,
                value: value
            })
            param.bind(this);
            this.params.set(id, param);
        } else {
            const param = this.getParam(id);
            if (param) {
                param.value = value;
                param.bind(this);
            }
        }
        return this;
    }
    getParam(id) {
        return this.params.get(id);
    }
    removeParam(id) {
        let param = this.getParam(id);
        if (param) {
            param.unBind(this);
            let flag = param.destroy();
            if (flag) {
                param = null;
            }
        }
        this.params.delete(id);
        return this;
    }
    removeAllParam() {
        this.params.forEach((param, id) => {
            this.removeParam(id)
        })
    }
    has(id) {
        return this.params.has(id);
    }
    getAll() {
        return Array.from(this.params);
    }
    getAllParams() {
        return Array.from(this.params.values());
    }
}
export { ParamManager }